#include "roidlg.h"
#include "ui_roidlg.h"
#include <QSettings>
#include <QTextCodec>
RoiDlg::RoiDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RoiDlg)
{
    ui->setupUi(this);
}

RoiDlg::~RoiDlg()
{
    delete ui;
}

void RoiDlg::on_SetRoiABt_clicked()
{
    if(!m_bOpen){
        return;
    }


    QString iniFilePath = "Config/Product/" + m_sIniFilePath;
    QSettings configIniWrite(iniFilePath, QSettings::IniFormat);
    configIniWrite.setIniCodec(QTextCodec::codecForName("UTF_8"));
    QString sInfo;


    HalconCpp::HObject Rect, Contour;
    try
    {
        HalconCpp::ClearWindow(hv_WindowHandle);
        HalconCpp::DispObj(Hobj, hv_WindowHandle);

        HalconCpp::DrawRectangle1(hv_WindowHandle, &m_algPlaceA.x1, &m_algPlaceA.y1, &m_algPlaceA.x2, &m_algPlaceA.y2);
        HalconCpp::GenRectangle1(&Rect, m_algPlaceA.x1, m_algPlaceA.y1, m_algPlaceA.x2, m_algPlaceA.y2);
        HalconCpp::GenContourRegionXld(Rect, &Contour, "border");
        HalconCpp::SetColor(hv_WindowHandle, "red");
        HalconCpp::DispObj(Contour, hv_WindowHandle);

        HalconCpp::HTuple* m_value = &m_algPlaceA.x1;
        ui->EditRoiA_1->setText(QString::number(m_algPlaceA.x1.D()));
        ui->EditRoiA_2->setText(QString::number(m_algPlaceA.y1.D()));
        ui->EditRoiA_3->setText(QString::number(m_algPlaceA.x2.D()));
        ui->EditRoiA_4->setText(QString::number(m_algPlaceA.y2.D()));

        for (int i = 0; i < 4; i++)
        {
            sInfo = QString("AlgPlaceA_%1/parame%2").arg(m_nCCDNum).arg(i);
            configIniWrite.setValue(sInfo, m_value->D());
            m_value++;
        }
    }
    catch (HalconCpp::HException ex)
    {

    }
}


void RoiDlg::on_SetRoiBBt_clicked()
{
    if(!m_bOpen){
        return;
    }

    QString iniFilePath = "Config/Product/" + m_sIniFilePath;
    QSettings configIniWrite(iniFilePath, QSettings::IniFormat);
    configIniWrite.setIniCodec(QTextCodec::codecForName("UTF_8"));
    QString sInfo;

    HalconCpp::HObject Rect, Contour;
    try
    {
        HalconCpp::ClearWindow(hv_WindowHandle);
        HalconCpp::DispObj(Hobj, hv_WindowHandle);

        HalconCpp::DrawRectangle1(hv_WindowHandle, &m_algPlaceB.x1, &m_algPlaceB.y1, &m_algPlaceB.x2, &m_algPlaceB.y2);
        HalconCpp::GenRectangle1(&Rect, m_algPlaceB.x1, m_algPlaceB.y1, m_algPlaceB.x2, m_algPlaceB.y2);
        HalconCpp::GenContourRegionXld(Rect, &Contour, "border");
        HalconCpp::SetColor(hv_WindowHandle, "red");
        HalconCpp::DispObj(Contour, hv_WindowHandle);

        HalconCpp::HTuple* m_value = &m_algPlaceB.x1;
        ui->EditRoiB_1->setText(QString::number(m_algPlaceB.x1.D()));
        ui->EditRoiB_2->setText(QString::number(m_algPlaceB.y1.D()));
        ui->EditRoiB_3->setText(QString::number(m_algPlaceB.x2.D()));
        ui->EditRoiB_4->setText(QString::number(m_algPlaceB.y2.D()));

        for (int i = 0; i < 4; i++)
        {
            sInfo = QString("AlgPlaceB_%1/parame%2").arg(m_nCCDNum).arg(i);
            configIniWrite.setValue(sInfo, m_value->D());
            m_value++;
        }
    }
    catch (HalconCpp::HException ex)
    {

    }
}
